#include "linkeritem.h"
#include "ui_linkeritem.h"

LinkerItem::LinkerItem(QWidget *parent,bool isCondition) :
    QWidget(parent),
    ui(new Ui::LinkerItem)
{
    ui->setupUi(this);
    search = new SearchLineEdit(this,GameData::Instance().GetDataListModel());
    search->setPlaceholderText("填入数值名称");
    ui->searchLayout->addWidget(search);
    if(isCondition)
        InitCondition();
    else
        InitAction();
}

LinkerItem::~LinkerItem()
{
    delete ui;
    delete search;
}

Condition *LinkerItem::ReadCondition()
{
    DATATYPE t = GameData::Instance().GetDataType(search->text());
    //参照数据
    Data d;
    switch(t)
    {
    case INT_:
        d.i = ui->lineEdit->text().toInt();
        break;
    case FLOAT_:
        d.f = ui->lineEdit->text().toFloat();
        break;
    case STRING_:
        d.s = ui->lineEdit->text();
        break;
    case BOOL_:
        d.b = ui->radioButton->isChecked();
        break;
    case NONE_:
        return nullptr;
        break;
    default:break;
    }
    d.dataType = t;
    Condition* c = new Condition();
    c->data = d;
    //符号
    QString sym = ui->comboBox->currentText();
    if(sym == "=")
        c->symbol = EQUAL;
    else if(sym == ">")
        c->symbol = BIGGER;
    else if(sym == "<")
        c->symbol = LESS;
    else if(sym == ">=")
        c->symbol = BIGGEROREQUAL;
    else if(sym == "<=")
        c->symbol = LESSOREQUAL;
    //名字
    c->name = ui->nameEdit->text();
    //目标数值key
    c->key = search->text();

    return c;
}

Action *LinkerItem::ReadAction()
{
    QString key = search->text();
    DATATYPE t = GameData::Instance().GetDataType(key);
    //数值
    Data d;
    switch(t)
    {
    case INT_:
        d.i = ui->lineEdit->text().toInt();
        break;
    case FLOAT_:
        d.f = ui->lineEdit->text().toFloat();
        break;
    case STRING_:
        d.s = ui->lineEdit->text();
        break;
    case BOOL_:
        d.b = ui->radioButton->isChecked();
        break;
    case NONE_:
        return nullptr;
        break;
    default:break;
    }
    d.dataType = t;
    //操作
    QString act = ui->comboBox->currentText();
    DATAACT da;
    if(act == "=")
        da = EQU;
    else if(act == "+")
        da = ADD;
    else if(act == "-")
        da = SUB;
    else if(act == "x")
        da = MUL;
    else if(act == "/")
        da = DIV;

    return new Action(key,d,da);

}

void LinkerItem::WriteCondition(Condition *c)
{
    ui->nameEdit->setText(c->name);
    search->setText(c->key);
    DATATYPE dt = c->data.dataType;
    switch(dt)
    {
    case INT_:
        ui->lineEdit->setText(QString::number(c->data.i));
        break;
    case FLOAT_:
        ui->lineEdit->setText(QString::number(c->data.f));
        break;
    case STRING_:
        ui->lineEdit->setText(c->data.s);
        break;
    case BOOL_:
        ui->radioButton->setChecked(c->data.b);
        break;
    case NONE_:break;
    default:break;
    }
    //符号
    SYMBOL s = c->symbol;
    switch(s)
    {
    case EQUAL:
        ui->comboBox->setCurrentText("=");
        break;
    case BIGGER:
        ui->comboBox->setCurrentText(">");
        break;
    case LESS:
        ui->comboBox->setCurrentText("<");
        break;
    case BIGGEROREQUAL:
        ui->comboBox->setCurrentText(">=");
        break;
    case LESSOREQUAL:
        ui->comboBox->setCurrentText("<=");
        break;
    default:break;
    }
}

void LinkerItem::WriteAction(Action *a)
{
    search->setText(a->key);
    DATATYPE dt = a->data.dataType;
    switch(dt)
    {
    case INT_:
        ui->lineEdit->setText(QString::number(a->data.i));
        break;
    case FLOAT_:
        ui->lineEdit->setText(QString::number(a->data.f));
        break;
    case STRING_:
        ui->lineEdit->setText(a->data.s);
        break;
    case BOOL_:
        ui->radioButton->setChecked(a->data.b);
        break;
    case NONE_:
        break;
    default:
        break;
    }
    //符号
    DATAACT act = a->act;
    switch(act)
    {
    case EQU:
        ui->comboBox->setCurrentText("=");
        break;
    case ADD:
        ui->comboBox->setCurrentText("+");
        break;
    case SUB:
        ui->comboBox->setCurrentText("-");
        break;
    case DIV:
        ui->comboBox->setCurrentText("/");
        break;
    case MUL:
        ui->comboBox->setCurrentText("x");
        break;
    default:break;
    }
}

void LinkerItem::InitCondition()
{
    ui->lineEdit->setPlaceholderText("填入参照值");
    ui->comboBox->addItem(">");
    ui->comboBox->addItem("<");
    ui->comboBox->addItem(">=");
    ui->comboBox->addItem("<=");
}

void LinkerItem::InitAction()
{
    ui->lineEdit->setPlaceholderText("填入目标值");
    ui->comboBox->addItem("+");
    ui->comboBox->addItem("-");
    ui->comboBox->addItem("x");
    ui->comboBox->addItem("/");
    ui->nameEdit->hide();
}
